<?php
class Admin_LibvideoController extends Zend_Controller_Action {
	public function preDispatch() {
		$auth = Zend_Auth::getInstance ();
		if (! $auth->hasIdentity ()) {
			if ($this->_request->getActionName () != "login") {
				$this->_redirect ( "/admin/index/login" );
			}
		}
	}
	public function init() {
		/* Initialize action controller here */
		$auth = Zend_Auth::getInstance ();
		$infoUser = $auth->getIdentity ();
		$this->view->fullName = $infoUser->USERNAME;
		$this->_model = new Admin_Model_DbTable_LibraryVideo ();
		Zend_Layout::startMvc ( array (
				"layoutPath" => APPLICATION_PATH . "/layouts/scripts",
				"layout" => "admin" 
		) );
	}
	public function indexAction() {
		// action body
		$status = $this->getRequest ()->getParam ( "sorting" );
		$this->view->headTitle ( "Quản Lý Thư Viện Video", "PREPEND" );
		$this->view->title = "Quản lý thư viện video";
		if ($status == null)
			$lib = $this->_model->getAllLibVideo ();
		else
			$lib = $this->_model->getLibVideoByStatus ( $status );
		$paginator = Zend_Paginator::factory ( $lib );
		$paginator->setItemCountPerPage ( 9 );
		$paginator->setPageRange ( 5 );
		$currentPage = $this->_request->getParam ( "page", 1 );
		$paginator->setCurrentPageNumber ( $currentPage );
		$this->view->allLib = $paginator;
	}
	public function addAction() {
		$this->view->headTitle ( "Thêm Mới Thư Viện Video", "PREPEND" );
		$this->view->title = "Thêm mới thư viện Video";
		if ($this->getRequest ()->isPost ()) {
			$Name = $this->_getParam ( "LibName" );
			$DOn = date ( "Y-m-d", strtotime ( $this->_getParam ( "DateOn" ) ) );
			$DOff = date ( "Y-m-d", strtotime ( $this->_getParam ( "DateOff" ) ) );
			$Desc = $this->_getParam ( "Desc" );
			$Stt = $this->_getParam ( "stt" );
			$this->_model->addLibVideo ( $Name, $DOn, $DOff, $Desc, $Stt );
			$this->_redirect ( "admin/libvideo" );
		}
	}
	public function editAction() {
		$this->view->headTitle ( "Chỉnh Sửa Thư Viện Hình", "PREPEND" );
		$this->view->title = "Chỉnh sửa thư viện hình";
		$id = $this->_getParam ( "id" );
		$this->view->lib = $this->_model->getLibVideoByLibId ( $id, 1 ); // print_r($this->_model->getLibVideoByLibId($id,
		                                                                 // 1));die();
		if ($this->getRequest ()->isPost ()) {
			$Name = $this->_getParam ( "LibName" );
			$DOn = date ( "Y-m-d", strtotime ( $this->_getParam ( "DateOn" ) ) );
			$DOff = date ( "Y-m-d", strtotime ( $this->_getParam ( "DateOff" ) ) );
			$Desc = $this->_getParam ( "Desc" );
			$Stt = $this->_getParam ( "stt" );
			if ($Stt == 0) {
				$videos = new Default_Model_DbTable_Video ();
				$list = $videos->GetByVideoLibID ( $id );
				foreach ( $list as $l ) {
					$videos->UpStt ( $l ["ID"], 0 );
				}
				$this->_model->updateLibVideo ( $id, $Name, $DOn, $DOff, $Desc, $Stt );
			} else {
				$videos = new Default_Model_DbTable_Video ();
				$list = $videos->GetByVideoLibID ( $id );
				foreach ( $list as $l ) {
					$videos->UpStt ( $l ["ID"], 1 );
				}
				$this->_model->updateLibVideo ( $id, $Name, $DOn, $DOff, $Desc, $Stt );
			}
			$this->_redirect ( "admin/libvideo" );
		}
	}
	public function delAction() {
		$id = $this->_getParam ( "id" );
		$videos = new Default_Model_DbTable_Video ();
		$list = $videos->GetByVideoLibID ( $id );
		foreach ( $list as $l ) {
			$videos->RemoveVideo ( $l ["ID"] );
		}
		$this->_model->removeLibVideo ( $id );
		$this->_redirect ( "admin/libvideo" );
	}
	public function activeAction() {
		$id = $this->_getParam ( "id" );
		$this->_model->active ( $id );
		$libvideo = $this->_model->getLibVideoByLibId ( $id );
		$videos = new Default_Model_DbTable_Video ();
		$list = $videos->GetByVideoLibID ( $id );
		foreach ( $list as $l ) {
			$videos->UpStt ( $l ["ID"], $libvideo ["STATUS"] );
		}
		$this->_redirect ( "admin/libvideo" );
	}
}

